package com.my.blogs.personblog.controller;

import com.my.blogs.personblog.Service.RootService;
import com.my.blogs.personblog.enity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/root")
public class RootController {
    @Autowired
    private RootService rootService;

    @GetMapping("/select/all")
    public ResponseEntity<Result> selectAllUsers() {
        Result result;
        List<Root> roots = rootService.selectAllUsers();
        if (roots == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", roots);
        }
        return ResponseEntity.ok(result);

    }

    @GetMapping("/select/{id}")
    public ResponseEntity<Result> selectUserById(@PathVariable Integer id) {
        Result result;
        Root root = rootService.selectUserById(id);
        if (root == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", root);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{username}")
    public ResponseEntity<Result> selectUserByUsername(@PathVariable String username) {
        Result result;
        Root root = rootService.selectUserByUsername(username);
        if (root == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", root);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{email}")
    public ResponseEntity<Result> selectUserByEmail(@PathVariable String email) {
        Result result;
        Root root = rootService.selectUserByEmail(email);
        if (root == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", root);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{phone}")
    public ResponseEntity<Result> selectUserByPhone(@PathVariable String phone) {
        Result result;
        Root root = rootService.selectUserByPhone(phone);
        if (root == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", root);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/insert")
    public ResponseEntity<Result> insertUser(User user) {
        Result result;
        Boolean status = rootService.insertUser(user);
        if (!status) {
            result = new Result(Code.FAIL, "插入失败", null);
        } else {
            result = new Result(Code.SUCCESS, "插入成功", null);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update")
    public ResponseEntity<Result> updateUser(User user) {
        Result result;
        Boolean status = rootService.updateUser(user);
        if (!status) {
            result = new Result(Code.FAIL, "更新失败", null);
        } else {
            result = new Result(Code.SUCCESS, "更新成功", null);
        }
        return ResponseEntity.ok(result);

    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Result> deleteUser(@PathVariable Integer id) {
        Result result;
        Boolean status = rootService.deleteUser(id);
        if (!status) {
            result = new Result(Code.FAIL, "删除失败", null);
        } else {
            result = new Result(Code.SUCCESS, "删除成功", null);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/sort/{id}")
    public ResponseEntity<Result> sortById(@PathVariable Integer id) {
        Result result;
        List<Root> roots = rootService.sortById(id);
        if (roots == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", roots);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/page/{page}/{pageSize}")
    public ResponseEntity<Result> selectPage(@PathVariable Integer page, @PathVariable Integer pageSize) {
        Result result;
        List<Root> roots = rootService.selectPage(page, pageSize);
        if (roots == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", roots);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/information/select/all")
    public ResponseEntity<Result> selectInformation() {
        Result result;
        List<information> information = rootService.selectInformation();
        if (information == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", information);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/information/insert")
    public ResponseEntity<Result> insertInformation(information information) {
        Result result;
        information status = rootService.insertInformation(information);
        if (status == null) {
            result = new Result(Code.FAIL, "插入失败", null);
        } else {
            result = new Result(Code.SUCCESS, "插入成功", null);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/information/update")
    public ResponseEntity<Result> updateInformation(information information) {
        Result result;
        Boolean status = rootService.updateInformation(information);
        if (!status) {
            result = new Result(Code.FAIL, "更新失败", null);
        } else {
            result = new Result(Code.SUCCESS, "更新成功", null);
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/information/delete/{id}")
    public ResponseEntity<Result> deleteInformation(@PathVariable Integer id) {
        Result result;
        Boolean status = rootService.deleteInformation(id);
        if (!status) {
            result = new Result(Code.FAIL, "删除失败", null);
        } else {
            result = new Result(Code.SUCCESS, "删除成功", null);
        }
        return ResponseEntity.ok(result);
    }
}
